package com.yvan.serverless;

import com.google.common.base.Strings;
import com.yvan.YvanUtil;
import com.yvan.platform.JsonWapper;
import com.yvan.serverless.mvc.Model;
import com.yvan.serverless.mvc.ModelOps;
import com.yvan.serverless.utils.FileUtilExt;
import com.yvan.serverless.utils.HttpUtilExt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class ServerLessDesignServlet extends HttpServlet {
    @Autowired
    private DesignService designService;

    @Autowired
    private ServerLessProperties properties;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        String url = request.getRequestURI();

        url = FileUtilExt.normalizePath(url);


        // 设计时 /@
        if (properties.getDesign() != null &&
                !Strings.isNullOrEmpty(properties.getDesign().getUrl()) &&
                url.startsWith(properties.getDesign().getUrl())) {

            url = url.substring(properties.getDesign().getUrl().length());
            Object result = "";
            try {
                if (url.equalsIgnoreCase("/ok")) {
                    HttpUtilExt.writeToResponse(resp, "text/plain", "OK");
                    return;

                } else if (url.equalsIgnoreCase("/load_sql")) {
                    String namespace = request.getParameter("namespace");
                    String sqlId = request.getParameter("sqlId");
//                    result = yvanUIService.sqlLoad(namespace, sqlId);

                } else if (url.endsWith("/check_module_last_modify")) {
                    String modulePath = request.getParameter("path");
                    result = designService.moduleCheck(modulePath);

                } else if (url.equalsIgnoreCase("/module_content")) {
                    String modulePath = request.getParameter("path");
                    result = designService.moduleContent(modulePath);

                } else if (url.equalsIgnoreCase("/module_service_content")) {
                    String modulePath = request.getParameter("path");
                    result = designService.moduleServiceContent(modulePath);

                } else if (url.equalsIgnoreCase("/module_tree")) {
                    result = designService.moduleTree();

                } else if (url.equalsIgnoreCase("/meta")) {
                    resp.sendRedirect("/meta");
                    return;

                } else if (url.equalsIgnoreCase("/meta_reload")) {
                    resp.sendRedirect("/meta_reload");
                    return;

                } else if (url.equalsIgnoreCase("/sql_tree")) {
                    result = ModelOps.newSuccess();

                } else if (url.equalsIgnoreCase("/info")) {
                    result = designService.appInfo();

                } else {
                    HttpUtilExt.writeToResponse(resp, "text/text", "404");
                    resp.setStatus(404);
                    result = Model.newFail("not support");
                }

            } catch (Exception e) {
                result = Model.newFail(e.toString());
                resp.setStatus(500);
                log.error("YvanUIDesign", e);
            }

            HttpUtilExt.writeToResponse(resp, "application/json;charset=utf-8", YvanUtil.toJson(result));
            return;
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        String url = request.getRequestURI();

        Object result;
        try {
            if (url.endsWith("/save_module_content")) {
                try (InputStream is = request.getInputStream()) {
                    JsonWapper jw = new JsonWapper(is);
                    result = designService.moduleSave(jw);
                }

            } else if (url.endsWith("/save_module_service_content")) {
                try (InputStream is = request.getInputStream()) {
                    JsonWapper jw = new JsonWapper(is);
                    result = designService.moduleServiceSave(jw);
                }

            } else if (url.endsWith("/create_module")) {
                try (InputStream is = request.getInputStream()) {
                    JsonWapper jw = new JsonWapper(is);
                    result = designService.moduleCreate(jw);
                }

            } else if (url.endsWith("/delete_module")) {
                String modulePath = request.getParameter("path");
                result = designService.moduleDelete(modulePath);

            } else if (url.endsWith("/save_sql")) {
//                try (InputStream is = request.getInputStream()) {
//                    JsonWapper jw = new JsonWapper(is);
//                    result = designService.sqlSave(jw);
//                }
                throw new RuntimeException("not supported");

            } else {
                throw new RuntimeException("not supported");
            }

        } catch (Exception e) {
            result = Model.newFail(e.getMessage());
            resp.setStatus(500);
            log.error("yvanui", e);
        }

        HttpUtilExt.writeToResponse(resp, "application/json;charset=utf-8", YvanUtil.toJson(result));
    }

}
